<template>
  <!-- 审核表单 -->
  <ProjectInfo
    :preview="true"
    :projectId="`${route.query.projectId}`"
    :reportType="'CCC'"
    :sign="true"
    @preview-file="toPreviewJobSurvey"
  />
  <ContentWrap>
    <el-form ref="formRef" :model="reviewForm" label-position="top" size="default">
      <!-- 审核项目列表 - 网格布局 -->
      <div class="review-items-grid">
        <div v-for="(item, idx) in reviewForm.items" :key="idx" class="review-item">
          <div class="review-row">
            <div class="review-label">{{ item.label }}</div>
            <div class="review-result">
              <el-form-item :prop="`items.${idx}.result`" class="result-form-item">
                <el-radio-group v-model="item.result" size="default">
                  <el-radio :label="true">是</el-radio>
                  <el-radio :label="false">否</el-radio>
                </el-radio-group>
              </el-form-item>
            </div>
            <div class="review-opinion">
              <el-form-item :prop="`items.${idx}.opinion`" class="opinion-form-item">
                <el-input
                  v-model="item.opinion"
                  :rows="2"
                  maxlength="200"
                  placeholder="请输入审核意见"
                  show-word-limit
                  type="textarea"
                />
              </el-form-item>
            </div>
          </div>
        </div>
      </div>

      <!-- 其他意见和审核人总体意见 -->
      <el-row :gutter="20">
        <el-col :span="12">
          <el-form-item label="其他" prop="remarks">
            <el-input
              v-model="reviewForm.remarks"
              maxlength="500"
              placeholder="请输入其他意见或建议"
              show-word-limit
              type="textarea"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="审核人总体意见" prop="opinion">
            <el-input
              v-model="reviewForm.opinion"
              maxlength="500"
              placeholder="请输入审核人总体意见"
              show-word-limit
              type="textarea"
            />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
  </ContentWrap>
</template>

<script lang="ts" setup>
import { onMounted, reactive, ref, watch } from 'vue'
import { type FormInstance } from 'element-plus'
import { ProjectApi } from '@/api/dk/project'
import ContentWrap from '../../../components/ContentWrap/src/ContentWrap.vue'
import ProjectInfo from '@/views/project/list/ProjectInfo.vue'
import { useRoute, useRouter } from 'vue-router'

const route = useRoute()
const router = useRouter()

// 响应式数据
const formRef = ref<FormInstance>()
// 审核表单数据
const reviewForm = reactive({
  items: [
    {
      label: '采样/测量计划内容是否全面',
      result: true,
      opinion: ''
    },
    {
      label: '采样/测量范围是否与委托文件规定是否一致',
      result: true,
      opinion: ''
    },
    {
      label: '检测项目是否全面',
      result: true,
      opinion: ''
    },
    {
      label: '检测项目对应的空气采集器、采样/测量设备和样品保存方法与时限是否正确',
      result: true,
      opinion: ''
    },
    {
      label: '采样/测量时机、数量、方式、时间等是否符合标准、规范要求',
      result: true,
      opinion: ''
    }
  ],
  remarks: '',
  opinion: '',
  reviewer: '',
  reviewDate: ''
})

const toPreviewJobSurvey = () => {
  // 跳转到现场调查页面，并携带项目信息
  const { projectId, projectNo } = route.query
  router.push({
    path: '/previewFile',
    query: {
      projectId: projectId,
      projectNo: projectNo,
      operationType: 3
    }
  })
}

/** 提交审核 */
const submitReview = async () => {
  if (!route.query.projectId) return
  const data = {
    surveyReview: {
      projectId: Number(route.query.projectId),
      ...reviewForm
    }
  }

  // 调用审核提交API
  await ProjectApi.updateSurveyReview(data)
  console.log('自动保存成功')
}

onMounted(async () => {
  const response = await ProjectApi.getSurveyReview(Number(route.query.projectId))
  // 检查是否有返回surveyReview数据，如果有则更新表单
  if (response && response.surveyReview) {
    const { surveyReview } = response

    // 更新表单数据
    reviewForm.items = surveyReview.items || reviewForm.items // 保留默认值如果API没返回
    reviewForm.remarks = surveyReview.remarks || ''
    reviewForm.opinion = surveyReview.opinion || ''
    reviewForm.reviewer = surveyReview.reviewer || ''
    reviewForm.reviewDate = surveyReview.reviewDate || ''
  } else {
    await submitReview()
  }
})

// 监听表单数据变化，自动保存
watch(
  () => reviewForm,
  () => {
    // 只有在数据加载完成后才开始监听保存
    submitReview()
  },
  { deep: true }
)
</script>

<style lang="scss" scoped>
/* 表单容器样式 */
.plan-review-form {
  max-width: 1000px;
  margin: 0 auto;
  padding: 20px;
  background: #f7f8fa;
  min-height: 100vh;
}

.card-block {
  background: #fff;
  border-radius: 8px;
  margin-bottom: 16px;
  padding: 20px 24px;
}

.block-title {
  display: flex;
  align-items: center;
  font-size: 16px;
  font-weight: 600;
  color: #2d8cf0;
  margin-bottom: 16px;
  padding-bottom: 8px;
  border-bottom: 1px solid #f0f0f0;

  .el-icon {
    margin-right: 8px;
  }
}

.info-content {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 12px 24px;
  margin-bottom: 0;
}

.info-item {
  .label {
    color: #888;
    margin-right: 8px;
    font-size: 14px;
  }

  .value {
    font-weight: 500;
    font-size: 14px;
    color: #333;
  }
}

/* 审核项目网格布局 */
.review-items-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 10px;
  margin-bottom: 20px;

  @media (max-width: 1024px) {
    grid-template-columns: 1fr;
    gap: 16px;
  }
}

/* 审核项目样式 */
.review-item {
  padding: 16px 20px;
  background: #fafbfc;
  border-radius: 8px;
  border: 1px solid #e5e6eb;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
  transition: all 0.3s ease;

  &:hover {
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
    border-color: #2d8cf0;
  }
}

.review-row {
  display: flex;
  align-items: center;
  gap: 20px;

  @media (max-width: 768px) {
    flex-direction: column;
    align-items: flex-start;
    gap: 12px;
  }
}

.review-label {
  flex: 1;
  font-weight: 500;
  color: #333;
  font-size: 14px;
  line-height: 1.4;
  min-width: 200px;
}

.review-result {
  flex-shrink: 0;
  width: 120px;

  .result-form-item {
    margin-bottom: 0;
  }

  :deep(.el-radio-group) {
    display: flex;
    gap: 16px;
    flex-direction: row;
  }

  :deep(.el-radio) {
    margin-right: 0;
  }
}

.review-opinion {
  flex: 1;
  min-width: 250px;

  .opinion-form-item {
    margin-bottom: 0;
  }
}

/* 表单样式优化 */
:deep(.el-form-item) {
  margin-bottom: 20px;
}

:deep(.el-form-item__label) {
  font-weight: 500;
  color: #333;
  line-height: 1.4;
}

:deep(.el-textarea__inner) {
  resize: vertical;
  min-height: 80px;
}

:deep(.el-input__count) {
  background: transparent;
}
</style>
